import Vue from 'vue'
import VueRouter from 'vue-router'

import Home from '../views/home/Home.vue'

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error)
}

Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    redirect: '/home/goods'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children: [
      {
        path: 'goods',
        name: 'HomeGoods',
        component: () => import('../views/home/HomeGoodsNew.vue'),
      },
      {
        path: 'user',
        name: 'user',
        component: () => import('../views/user/User.vue'),
        beforeEnter: (to, from, next) => {
          let token = Vue.$cookies.get('token');
          if (!token) {
            next('/login')
          }
          next();
        }
      },
      {
        path: 'tbPassword',
        name: 'TbPassword',
        component: () => import('../views/TbPassword.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('../views/search/Search.vue')
  },
  {
    path: '/couponInfo',
    name: 'CouponInfo',
    component: () => import('../views/CouponInfo.vue')
  },
  {
    path: '/tagItems',
    name: 'TagItems',
    component: () => import('../views/home/TagItems.vue')
  },
  {
    path: '/itemInfo',
    name: 'ItemInfo',
    component: () => import('../views/item/ItemInfo.vue'),
    props: true
  },
  {
    path: '/eleme',
    name: 'Eleme',
    component: () => import('../views/Eleme.vue')
  },
  {
    path: '/order',
    name: 'UserOrder',
    component: () => import('../views/user/UserOrder.vue')
  }
]



const router = new VueRouter({
  routes,
  scrollBehavior (to, from, savedPosition) {
    return { x: 0, y: 0 }
  }
})
export default router
